﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim OleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("connectionString").ConnectionString)
            OleDbConn.Open()
            Dim sqlString As String = "SELECT * FROM UserProfile WHERE JoeId=@f6"
            Dim cmd As OleDbCommand = New OleDbCommand(sqlString, OleDbConn)
            cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

            Dim dr = cmd.ExecuteReader()

            dr.Read()

            If Not IsDBNull(dr("GivenName")) Then
                tb_GivenName.Text = dr("GivenName")
            End If
            If Not IsDBNull(dr("FamilyName")) Then
                tb_FamilyName.Text = dr("FamilyName")

            End If


            If Not IsDBNull(dr("Gender")) Then
                tb_Gender.Text = dr("Gender")
            End If





            If Not IsDBNull(dr("Portrait")) Then
                ImageMap1.ImageUrl = dr("Portrait")
            End If


            If Not IsDBNull(dr("DateOfBirth")) Then
                Dim userDoB As DateTime = dr("DateOfBirth")

                ddl_Year.SelectedValue = userDoB.Year
                ddl_Month.SelectedValue = userDoB.Month
                ddl_Day.SelectedValue = userDoB.Day
            End If



          
        End If
    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_Portrait.FileName)
        If fup_Portrait.HasFile Then
            fup_Portrait.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If
        Dim userDoB As DateTime = New DateTime(CInt(ddl_Year.SelectedValue), CInt(ddl_Month.SelectedValue), CInt(ddl_Day.SelectedValue))

        Dim OleDbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("connectionString").ConnectionString)
        OleDbConn.Open()
        Dim sqlString As String = "UPDATE UserProfile SET GivenName=@f1, FamilyName=@f2, Portrait=@f3, DateOfBirth=@f4, Gender=@f5 WHERE JoeId=@f6"
        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, OleDbConn)
        cmd.Parameters.AddWithValue("@f1", tb_GivenName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_FamilyName.Text)

        If fup_Portrait.HasFile Then
            cmd.Parameters.AddWithValue("@f3", "Pictures/" & newFileName)
        Else
            cmd.Parameters.AddWithValue("@f3", ImageMap1.ImageUrl.ToString())
        End If


        cmd.Parameters.AddWithValue("@f4", userDoB.Date)
        cmd.Parameters.AddWithValue("@f5", tb_Gender.Text)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)

        cmd.ExecuteNonQuery()
        OleDbConn.Close()
        cmd.Dispose()
        OleDbConn.Dispose()

        Response.Redirect("editprofile.aspx")
    End Sub
    Function getSuffix(ByVal fn As String)
        Dim fileparts = fn.Split(".")
        Dim Suffix = fileparts(fileparts.Length - 1)
        Return "." & Suffix
    End Function

End Class


